package com.itheima.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单服务
 */
@Service(interfaceClass = MenuService.class,weight = 100)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Map> findMenuparentMenuId() {
        String queryString=null;
        List<Menu> all = menuDao.findAll(queryString);
        List<Map> list = new ArrayList<>();
        for (Menu menu : all) {
            Map map = new HashMap();
            map.put("label",menu.getName());
            map.put("value",menu.getId());
            map.put("priority",menu.getPriority());
            List<Menu> children = menu.getChildren();
            if(children!=null){
                List<Map> list2 = new ArrayList<>();
                for (Menu child : children) {
                    Map map2 = new HashMap();
                    map2.put("label",child.getName());
                    map2.put("value",child.getId());
                    map2.put("priority",child.getPriority());
                    List<Menu> children2 = child.getChildren();
                    if(children2!=null){
                        List<Map> list3 = new ArrayList<>();
                        for (Menu child2 : children2) {
                            Map map3 = new HashMap();
                            map3.put("label",child2.getName());
                            map3.put("value",child2.getId());
                            map3.put("priority",child2.getPriority());
                            list3.add(map3);
                        }
                        map2.put("children",list3);
                    }
                    list2.add(map2);
                }
                map.put("children",list2);
            }
            list.add(map);
        }
        return list;
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        List<Menu> all = menuDao.findAll(queryString);
        PageInfo pageInfo = new PageInfo(all);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    //回显选中的父菜单id和父菜单的父菜单的id
    @Override
    public List<Integer> findParentIdsByMenuId(Integer id) {
        List<Integer> list = new ArrayList<>();
        Menu thrid = menuDao.findById(id);
        if(thrid.getParentMenuId()!=null){
            list.add(thrid.getParentMenuId());
            Menu second = menuDao.findById(thrid.getParentMenuId());
            if(second.getParentMenuId()!=null){
                list.add(second.getParentMenuId());
            }
        }
        return list;
    }

    @Override
    public List<Map> findMenuIdAndDisabled(Integer id) {
        List<Map> maps = new ArrayList<>();
        Menu menu = menuDao.findById(id);
        Integer parentMenuId = menu.getParentMenuId();
        List<Menu> list=menuDao.findByParentMenuId2(parentMenuId);
        if(list!=null){
            for (Menu menu1 : list) {
                Map map = new HashMap();
                Integer id1 = menu1.getId();
                String name = menu1.getName();
                map.put("label",name);
                map.put("value",id1);
                map.put("priority",menu1.getPriority());
                maps.add(map);
            }
        }
        return maps;
    }

    @Override
    public void delete(Integer id) {
        List<Menu> menuList = menuDao.findByParentMenuId3(id);
        //有子菜单，抛出异常，不允许删除
        if(menuList!=null&&menuList.size()!=0){
            throw new RuntimeException("有子菜单，不允许删除");
        }
        //如果菜单关联角色表，不让删除
        Integer count=menuDao.countAssociate(id);
        if(count!=0){
            throw new RuntimeException("有角色关联菜单，不允许删除");
        }
        menuDao.delete(id);
    }

    @Override
    public void add(Integer id, Menu menu) {
        //说明是新增一级菜单
        if(id==999999){
            //优先级不选默认第一个
            if(menu.getPriority()==null){
                //获取当前等级最小优先级
                Menu min=menuDao.getMinPriorityMenuByLevel(menu.getLevel());
                //新增为第一个一个菜单
                if(min==null){
                    menu.setPriority(1);
                    menu.setPath("/1");
                    menuDao.add(menu);
                    return;
                }
                menuDao.setLaterPriority(min.getPriority());
                //动态设置path
                List<Menu> list=menuDao.findByLevelAndPriority(menu.getLevel(),min.getPriority());
                List<Menu> newList = new ArrayList<>();
                for (Menu menu1 : list) {
                    String laterPath = menu1.getPath().substring(1);
                    menu1.setPath("/"+(Integer.parseInt(laterPath)+1));
                    newList.add(menu1);
                    //置空path
                    menuDao.updatePathNullById(menu1.getId());
                }
                for (Menu menu1 : newList) {
                    menuDao.updatePathById(menu1.getPath(),menu1.getId());
                }
                menu.setPriority(min.getPriority());
                menu.setPath(min.getPath());
                menuDao.add(menu);
                return;
            }
            //优先级选不是默认第一个,说明传过来的数据带有优先级
            Menu min=menuDao.getMinPriorityMenuByPriority(menu.getPriority());
            menuDao.setLaterPriority(min.getPriority());
            //动态设置path
            List<Menu> list=menuDao.findByLevelAndPriority(menu.getLevel(),min.getPriority());
            List<Menu> newList = new ArrayList<>();
            for (Menu menu1 : list) {
                String laterPath = menu1.getPath().substring(1);
                menu1.setPath("/"+(Integer.parseInt(laterPath)+1));
                newList.add(menu1);
                //置空path
                menuDao.updatePathNullById(menu1.getId());
            }
            for (Menu menu1 : newList) {
                menuDao.updatePathById(menu1.getPath(),menu1.getId());
            }
            menu.setPriority(min.getPriority());
            menu.setPath(min.getPath());
            menuDao.add(menu);
            return;
        }

        Menu ParentMenu=menuDao.findById(menu.getParentMenuId());
        //id不是99999，说明不是一级菜单
        //优先级不选默认第一个
        if(menu.getPriority()==null){
            //获取当前等级最小优先级
            Menu min=menuDao.getMinPriorityMenuByLevel(menu.getLevel());
            //新增为第一个一个菜单
            if(min==null){
                menuDao.setLaterPriority(ParentMenu.getPriority()+1);//给新增节点留个位置
                //动态设置path
                List<Menu> list=menuDao.findByLevelAndPriority(menu.getLevel(),min.getPriority());
                List<Menu> newList = new ArrayList<>();
                for (Menu menu1 : list) {
                    String laterPath = menu1.getPath().substring(1);
                    menu1.setPath("/"+(Integer.parseInt(laterPath)+1));
                    newList.add(menu1);
                    //置空path
                    menuDao.updatePathNullById(menu1.getId());
                }
                for (Menu menu1 : newList) {
                    menuDao.updatePathById(menu1.getPath(),menu1.getId());
                }
                menu.setPriority(ParentMenu.getPriority()+1);
                menu.setPath(ParentMenu.getPath()+"-1");
                menuDao.add(menu);
                return;
            }
            menuDao.setLaterPriority(min.getPriority());
            //动态设置path
            List<Menu> list=menuDao.findByLevelAndPriority(menu.getLevel(),min.getPriority());
            List<Menu> newList = new ArrayList<>();
            for (Menu menu1 : list) {
                String laterPath = menu1.getPath().substring(menu1.getPath().lastIndexOf('-')+1);
                menu1.setPath(ParentMenu.getPath()+(Integer.parseInt(laterPath)+1));
                newList.add(menu1);
                //置空path
                menuDao.updatePathNullById(menu1.getId());
            }
            for (Menu menu1 : newList) {
                menuDao.updatePathById(menu1.getPath(),menu1.getId());
            }
            menu.setPriority(min.getPriority());
            menu.setPath(min.getPath());
            menuDao.add(menu);
            return;
        }
        //优先级选不是默认第一个,说明传过来的数据带有优先级
        Menu min=menuDao.getMinPriorityMenuByPriority(menu.getPriority());
        menuDao.setLaterPriority(min.getPriority());
        //动态设置path
        List<Menu> list=menuDao.findByLevelAndPriority(menu.getLevel(),min.getPriority());
        List<Menu> newList = new ArrayList<>();
        if(ParentMenu==null){
            for (Menu menu1 : list) {
                String laterPath = menu1.getPath().substring(1);
                menu1.setPath("/"+((Integer.parseInt(laterPath)+1)));
                newList.add(menu1);
                //置空path
                menuDao.updatePathNullById(menu1.getId());
            }
            for (Menu menu1 : newList) {
                menuDao.updatePathById(menu1.getPath(),menu1.getId());
            }
            menu.setPriority(min.getPriority());
            menu.setPath(min.getPath());
            menuDao.add(menu);
            return;
        }


        for (Menu menu1 : list) {
            String laterPath = menu1.getPath().substring(menu1.getPath().lastIndexOf('-')+1);
            menu1.setPath(ParentMenu.getPath()+(Integer.parseInt(laterPath)+1));
            newList.add(menu1);
            //置空path
            menuDao.updatePathNullById(menu1.getId());
        }
        for (Menu menu1 : newList) {
            menuDao.updatePathById(menu1.getPath(),menu1.getId());
        }
        menu.setPriority(min.getPriority());
        menu.setPath(min.getPath());
        menuDao.add(menu);
        return;

    }

    @Override
    public void edit(Menu menu) {
        if(menu.getPriority()==null){
            Menu min=menuDao.getMinPriorityMenuByLevel(menu.getLevel());
            menuDao.setLaterPriority(min.getPriority());
            menu.setPriority(min.getPriority());
        }else{
            Integer priority = menu.getPriority();
            menuDao.setLaterPriority(priority);
        }
        menuDao.setPriorityById(menu.getId(),menu.getPriority());
        //动态设置path
        List<Menu> list=menuDao.findByLevelAndPriority(menu.getLevel(),menu.getPriority()-1);
        List<Menu> firstList = new ArrayList<>();
        if(menu.getParentMenuId()==null){
            //一级菜单
            int i = 1;
            for (Menu menu1 : list) {
                menu1.setPath("/"+i);
                firstList.add(menu1);
                menuDao.updatePathById(menu1.getPath(),menu1.getId());
                i++;
            }
            for (Menu menu1 : firstList) {
                //二级菜单
                List<Menu> secondList = menuDao.findByParentMenuId3(menu1.getId());
                int j=1;
                for (Menu menu2 : secondList) {
                    menu2.setPath(menu1.getPath()+"-"+j);
                    menuDao.updatePathById(menu2.getPath(),menu2.getId());
                    List<Menu> thridList = menuDao.findByParentMenuId3(menu2.getId());
                    int k=1;
                    for (Menu menu3 : thridList) {
                        //三级菜单
                        menu2.setPath(menu1.getPath()+"-"+j);
                        menuDao.updatePathById(menu2.getPath(),menu2.getId());
                        k++;
                    }
                    j++;
                }
            }

        }else {
            firstList = menuDao.findByLevelAndPriority(menu.getLevel(), menu.getPriority() - 1);
            for (Menu menu1 : firstList) {
                //二级菜单
                List<Menu> secondList = menuDao.findByParentMenuId3(menu1.getId());
                int j = 1;
                for (Menu menu2 : secondList) {
                    menu2.setPath(menu1.getPath() + "-" + j);
                    menuDao.updatePathById(menu2.getPath(), menu2.getId());
                    List<Menu> thridList = menuDao.findByParentMenuId3(menu2.getId());
                    int k = 1;
                    for (Menu menu3 : thridList) {
                        //三级菜单
                        menu2.setPath(menu1.getPath() + "-" + j);
                        menuDao.updatePathById(menu2.getPath(), menu2.getId());
                        k++;
                    }
                    j++;
                }
            }
        }
        System.out.println(menu.getParentMenuId());
        menuDao.edit(menu);
    }


    //根据登录用户名获取菜单列表信息
    @Override
    public List<Map> findAllByUsername(String name) {
        List<Menu> all=menuDao.findAllByUsername(name);
        List<Map> list = new ArrayList<>();
        for (Menu menu : all) {
            Map map = new HashMap();
            map.put("path",menu.getPath());
            map.put("title",menu.getName());
            map.put("icon",menu.getIcon());
            map.put("linkUrl",menu.getLinkUrl());
            List<Menu> children = menuDao.findMenuListByParentMenuId(menu.getId(),name);
            if(children!=null){
                List<Map> list2 = new ArrayList<>();
                for (Menu child : children) {
                    Map map2 = new HashMap();
                    map2.put("path",child.getPath());
                    map2.put("title",child.getName());
                    map2.put("icon",child.getIcon());
                    map2.put("linkUrl",child.getLinkUrl());
                    List<Menu> children2 = menuDao.findMenuListByParentMenuId(child.getId(),name);
                    if(children2!=null){
                        List<Map> list3 = new ArrayList<>();
                        for (Menu child2 : children2) {
                            Map map3 = new HashMap();
                            map3.put("path",child2.getPath());
                            map3.put("title",child2.getName());
                            map3.put("icon",child2.getIcon());
                            map3.put("linkUrl",child2.getLinkUrl());
                            list3.add(map3);
                        }
                        map2.put("children",list3);
                    }
                    list2.add(map2);
                }
                map.put("children",list2);
            }
            list.add(map);
        }
        return list;
    }


}
